home *** CD-ROM | disk | FTP | other *** search
- #include <Files.h>
- #include <AppleEvents.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <SegLoad.h>
- #include <OSUtils.h>
- #include <Memory.h>
- #include <Fonts.h>
- #include <Events.h>
- #include <OSEvents.h>
- #include <TextEdit.h>
- #include <Dialogs.h>
- #include <Aliases.h>
- #include <Folders.h>
- #include <Errors.h>
- #include <GestaltEqu.h>
- #include <StandardFile.h>
-
- #include <fcntl.h>
- #include <a.out.h>
- #include <sys/exec.h>
-
- #define TrapMask 0x0800
-
- static short NumToolboxTraps( void )
- {
- if (GetToolTrapAddress(0xA86E) ==
- GetToolTrapAddress(0xAA6E))
- return(0x0200);
- else
- return(0x0400);
- }
-
- static TrapType GetTrapType(short theTrap)
- {
-
- if ((theTrap & TrapMask) > 0)
- return(ToolTrap);
- else
- return(OSTrap);
-
- }
-
- static Boolean TrapAvailable(short theTrap)
- {
-
- TrapType tType;
-
- tType = GetTrapType(theTrap);
- if (tType == ToolTrap)
- theTrap = theTrap & 0x07FF;
- if (theTrap >= NumToolboxTraps())
- theTrap = 0xA89F;
-
- return ((tType == ToolTrap?GetToolTrapAddress(theTrap):GetOSTrapAddress(theTrap))!=
- GetToolTrapAddress(0xA89F));
- }
-
-
- #define _CacheFlushTrap 0xA0BD
-
- void FlushCache(void)
- {
- if (TrapAvailable( _CacheFlushTrap))
- asm
- {
- dc.w _CacheFlushTrap
- }
- }
-
- void fatal(char *str)
- {
- Str255 errmsg;
- static struct {
- char privates[76];
- long randSeed;
- BitMap screenBits;
- Cursor arrow;
- Pattern dkGray;
- Pattern ltGray;
- Pattern gray;
- Pattern black;
- Pattern white;
- GrafPtr thePort;
- }myqd;
- InitGraf(&myqd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
- *errmsg = strlen(str);
- BlockMove(str, 1+errmsg, *errmsg);
- ParamText("\pfatal error ", errmsg, 0,0);
- Alert(129, 0);
- SysBeep(1);
- ExitToShell();
- }
-
- void *emptyblock(size_t sz)
- {
- void *ptr = NewPtrClear(sz);
- if (!ptr) fatal("Out of memory");
- return ptr;
- }
-
- int read(int fd, void *buf, unsigned size)
- {
- int i;
- OSErr err;
- IOParam pb;
- pb.ioCompletion = 0;
- pb.ioRefNum = fd;
- pb.ioBuffer = buf;
- pb.ioReqCount = size;
- pb.ioPosMode = fsAtMark;
- PBReadSync((ParmBlkPtr)&pb);
- err = (pb.ioResult);
- return pb.ioActCount;
- }
-
- int newproc(FSSpec name1, int resource_size)
- {
- void (*entry)();
- struct exec header;
- OSErr err;
- int fd,refnum;
- HParamBlockRec pb;
- if (!*name1.name) return -1;
- pb.ioParam.ioNamePtr = name1.name;
- pb.ioParam.ioVRefNum = name1.vRefNum;
- pb.fileParam.ioDirID = name1.parID;
- pb.ioParam.ioPermssn = fsRdPerm;
- err = (PBHOpenSync(&pb));
- if ( err ) return -1;
- refnum = pb.ioParam.ioRefNum;
- if (refnum < 0) fatal("Could not open exec file");
- read(refnum, (void *)&(header), sizeof(header));
- if (N_BADMAG(header)) fatal("Bad magic number");
- if (header.a_trsize || header.a_drsize)
- {
- void *storage;
- long stack_headroom = 1<<16;
- resource_size = loader_main(refnum, &(header),resource_size);
- if (resource_size == -1) fatal("Could not load program");
- }
- else fatal("Program not relocatable");
- {
- FCBPBRec pb;
- pb.ioRefNum = refnum;
- pb.ioCompletion = 0;
- pb.ioVRefNum = name1.vRefNum;
- PBCloseSync((ParmBlkPtr)&pb);
- }
- entry = (void (*)())(header.a_entry);
- FlushCache();
- entry();
- ExitToShell();
- }
-
- OSErr CurrentProcessLocation(FSSpec *applicationSpec)
- {
- ProcessSerialNumber currentPSN;
- ProcessInfoRec info;
-
- currentPSN.highLongOfPSN = 0;
- currentPSN.lowLongOfPSN = kCurrentProcess;
- info.processInfoLength = sizeof(ProcessInfoRec);
- info.processName = NULL;
- info.processAppSpec = applicationSpec;
- return ( GetProcessInformation(¤tPSN, &info) );
- }
-
- void main(void)
- {
- FSSpec applicationSpec;
- OSErr err = CurrentProcessLocation(&applicationSpec);
- CInfoPBRec cPB;
- cPB.hFileInfo.ioNamePtr = applicationSpec.name;
- cPB.hFileInfo.ioVRefNum = applicationSpec.vRefNum;
- cPB.hFileInfo.ioDirID = applicationSpec.parID;
- cPB.hFileInfo.ioFDirIndex = 0;
-
- if (!PBGetCatInfoSync(&cPB))
- {
- long size = cPB.hFileInfo.ioFlLgLen;
- if (size) newproc(applicationSpec, size);
- else
- {
- static struct {
- char privates[76];
- long randSeed;
- BitMap screenBits;
- Cursor arrow;
- Pattern dkGray;
- Pattern ltGray;
- Pattern gray;
- Pattern black;
- Pattern white;
- GrafPtr thePort;
- }myqd;
- long aProcID;
- SFReply macSFReply;
- Point where;
- SFTypeList typeList;
-
- InitGraf(&myqd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- typeList[0]='TEXT'; /* file type to search for */
- typeList[1]='ŎŎ'; /* file type to search for */
- where.h=20; where.v=90; /* SF dialog window position */
-
- SFGetFile(where, /* put topLeft corner of dialog box here */
- (ConstStr255Param)0, /* always 0 on SFGetFile() */
- 0, /* address of custom filter */
- -1, /* looking for all file types */
- typeList, /* start of list of file types */
- 0, /* use standard dialog handler */
- &macSFReply); /* put results here */
-
- if ( macSFReply.good )
- {
- WDPBRec wdpb;
- wdpb.ioNamePtr = 0;
- wdpb.ioVRefNum = macSFReply.vRefNum;
- wdpb.ioWDIndex =0;
- wdpb.ioWDProcID =0;
- wdpb.ioWDVRefNum =0;
- PBGetWDInfoSync( &wdpb );
- applicationSpec.parID = wdpb.ioWDDirID;
- applicationSpec.vRefNum = wdpb.ioWDVRefNum;
- BlockMove(macSFReply.fName, applicationSpec.name, macSFReply.fName[0]+1);
- cPB.hFileInfo.ioNamePtr = applicationSpec.name;
- cPB.hFileInfo.ioVRefNum = applicationSpec.vRefNum;
- cPB.hFileInfo.ioDirID = applicationSpec.parID;
- cPB.hFileInfo.ioFDirIndex = 0;
-
- if (!PBGetCatInfoSync(&cPB))
- {
- long size = cPB.hFileInfo.ioFlLgLen;
- newproc(applicationSpec, size);
- }
- }
- }
- }
- }
-